import {getFeedback} from '../../api/complaint'

Page({
  data: {
    feedbackList: [], // 反馈列表数据
    currentPage: 1, // 当前页码
    pageSize: 10, // 每页条数
    totalCount: 0, // 总条数
    isLoading: false, // 是否加载中
    hasMore: true, // 是否有更多数据
    currentFilter: "全部状态", // 当前筛选条件
    currentDate: "全部时间", // 当前日期筛选
    hasUnread: false, // 是否有未读
    unreadCount: 0 // 未读数量
  },

  onLoad() {
    // 初始加载数据
    this.loadFeedbackList();
  },

  onPullDownRefresh() {
    // 下拉刷新，重置数据
    this.setData({
      currentPage: 1,
      feedbackList: [],
      hasMore: true
    }, () => {
      this.loadFeedbackList(() => {
        wx.stopPullDownRefresh();
      });
    });
  },

  onReachBottom() {
    // 上拉加载更多
    if (this.data.hasMore && !this.data.isLoading) {
      this.setData({
        currentPage: this.data.currentPage + 1
      }, () => {
        this.loadFeedbackList();
      });
    }
  },
  formatDate(dateStr) {
    const date = new Date(dateStr);
    const year = date.getFullYear();
    // 月份从 0 开始，所以要 +1
    const month = (date.getMonth() + 1).toString().padStart(2, '0'); 
    const day = date.getDate().toString().padStart(2, '0');
    const hours = date.getHours().toString().padStart(2, '0');
    const minutes = date.getMinutes().toString().padStart(2, '0');
    const seconds = date.getSeconds().toString().padStart(2, '0');
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  },

  // 加载反馈列表
  async loadFeedbackList() {
    const { currentPage, pageSize, currentFilter, currentDate } = this.data;
    
    this.setData({ isLoading: true });
    const res=await getFeedback()
    let list=res.data.list
    for (let i = 0; i < list.length; i++) {
      const element = list[i];
      element.createAt=this.formatDate(element.createAt)
      if(element.feedImg!=null){
        element.imglist=element.feedImg.split(",")
      }else{
        element.imglist=[]
      }
      console.log(element.imglist)
    }
    this.setData({
      feedbackList:list
    })
    console.log(this.data.feedbackList)
  },

  // 显示筛选弹窗
  showFilter() {
    const that = this;
    wx.showActionSheet({
      itemList: ["全部状态", "未处理", "已处理"],
      success(res) {
        const selected = ["全部状态", "未处理", "已处理"][res.tapIndex];
        if (selected !== that.data.currentFilter) {
          that.setData({
            currentFilter: selected,
            currentPage: 1,
            feedbackList: [],
            hasMore: true
          }, () => {
            that.loadFeedbackList();
          });
        }
      }
    });
  },

  // 显示日期选择
  showDatePicker() {
    const that = this;
    wx.showActionSheet({
      itemList: ["全部时间", "今天", "近7天", "近30天"],
      success(res) {
        const selected = ["全部时间", "今天", "近7天", "近30天"][res.tapIndex];
        if (selected !== that.data.currentDate) {
          that.setData({
            currentDate: selected,
            currentPage: 1,
            feedbackList: [],
            hasMore: true
          }, () => {
            that.loadFeedbackList();
          });
        }
      }
    });
  },

  // 查看反馈详情
  viewDetail(e) {
    const feedbackItem = e.currentTarget.dataset.item;
    // 跳转到详情页，携带反馈ID
    wx.navigateTo({
      url: `/pages/feedbackDetail/feedbackDetail?id=${feedbackItem.id}`
    });
  }
});
